package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.EaseLookup;
	import com.greensock.easing.Elastic;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class Util
	{
		
		public function Util()
		{
		}
		
		public static function getRandomStoreNoun():String{
			var nouns:Array = [
				'Tools',
				'Guitars',
				'Dogs',
				'Shirts',
				'Sweaters',
				'Ties',
				'Decors',
				'Burgers',
				'Pizzas',
				'Rings',
				'Clocks',
				'Pictures',
				'Games',
				'Radios',
				'TVs',
				'Shoes',
				'Hats',
				'Drinks',
				'Desserts',
				'Cakes',
				'Birds',
				'Glasses',
				'Toys',
				'Flowers',
				];
			
			return nouns[Math.floor(Math.random()*nouns.length)];
		}
		
		public static function clamp(point:Point, size:int):Point{
			var point:Point = new Point(Math.floor(point.x/size)*size, Math.floor(point.y/size)*size);
			return point;
		}
		
		public static function getGridSprite(squareSize:int, gridDimensions:Point,color:uint=0,alpha:Number=1):Sprite{
			var grid:Sprite = new Sprite();
			grid.graphics.lineStyle(1,color,alpha);
			var i:int;
			for(i = 0; i <= gridDimensions.x; i++){
				grid.graphics.moveTo(i*squareSize, 0);
				grid.graphics.lineTo(i*squareSize, gridDimensions.y*squareSize);
			}
			
			for(i = 0; i <= gridDimensions.y; i++){
				grid.graphics.moveTo(0, i*squareSize);
				grid.graphics.lineTo(gridDimensions.x*squareSize,i*squareSize);
			}
			
			return grid;
		}
		
		public static function makeButton(imageUrl:String):Sprite{
			var shopIconSprite:Sprite = new Sprite();
			var shopIcon:DisplayObject = ImageManager.getImage(imageUrl);
			var shopButton:Sprite = Util.getRoundRectSprite(shopIcon.width+10,shopIcon.height+5,0,.7,20);
			shopIcon.x = -shopIcon.width/2;
			shopIcon.y = -shopIcon.height/2;
			shopIconSprite.addChild(shopIcon);
			shopIconSprite.x = shopButton.width/2;
			shopIconSprite.y = shopButton.height/2;
			shopButton.addChild(shopIconSprite);
			shopButton.addEventListener(MouseEvent.MOUSE_OVER, function():void{
				TweenLite.to(shopIconSprite, .2, {scaleX: 1.3, scaleY: 1.3, ease: Back.easeOut});
			});
			shopButton.addEventListener(MouseEvent.MOUSE_OUT, function():void{
				TweenLite.to(shopIconSprite, .2, {scaleX: 1, scaleY: 1, ease: Elastic.easeOut});
			});		
			
			return shopButton;
		}
		
		public static function glowMatchSet(matchSet:MatchSet, color:uint):void{
			var i:int, l:int;
			l = matchSet.gameObjects.length;
			for(i = 0; i < l; i++){
				if(color == 0){
					matchSet.gameObjects[i].display.filters = [];
				} else {
					matchSet.gameObjects[i].display.filters = [new GlowFilter(color,1,6,6,6)];	
				}
				
			}
		}
		
		
		public static function getRoundRectSprite(width:int,height:int,color:uint=0x000000,alpha:Number=1.0, radius:Number=5, x:int=0,y:int=0):Sprite{
			var rect:Sprite = new Sprite();
			rect.graphics.beginFill(color, alpha);
			rect.graphics.drawRoundRect(x,y,width,height,radius,radius);
			rect.graphics.endFill()
			return rect;
		}
		
		public static function getRectSprite(width:int,height:int,color:uint=0x000000,alpha:Number=1.0,x:int=0,y:int=0, line:Boolean = false):Sprite{
			var rect:Sprite = new Sprite();
			if(line){
				rect.graphics.lineStyle(1);
				rect.graphics.lineTo(width,0);
				rect.graphics.lineTo(width,height);
				rect.graphics.lineTo(0,height);
				rect.graphics.lineTo(0,0);
			}
			rect.graphics.beginFill(color, alpha);
			rect.graphics.drawRect(x,y,width,height);
			rect.graphics.endFill()
			return rect;
		}	
		
		public static function applyStyleH1(tf:TextField,color:uint=0xffffff):void{
			tf.defaultTextFormat = new TextFormat('Verdana',24,color,true);
			tf.selectable = false;
			tf.filters = [new GlowFilter(0,1,4,4,5)];			
		}
		
		public static function applyStyleH2(tf:TextField, color:uint=0xffffff):void{
			tf.defaultTextFormat = new TextFormat('Verdana',18,color,true);
			tf.selectable = false;
			tf.filters = [new GlowFilter(0,1,4,4,4)];			
		}
		
		public static function applyStyleH3(tf:TextField, color:uint=0xffffff):void{
			tf.defaultTextFormat = new TextFormat('Verdana',14,color,true);
			tf.selectable = false;
			tf.filters = [new GlowFilter(0,1,3,3,3)];			
		}	
		
		public static function scale(display:DisplayObject, starting:Number, ending:Number, time:Number, startingAlpha:Number=1, endingAlpha:Number=1,ease:Function=null):void{
			display.alpha = startingAlpha;
			var centerPoint:Point = new Point(display.x + display.width/2, display.y + display.height/2);

			display.scaleX = ending;
			display.scaleY = ending;
			var endingPos:Point = new Point(centerPoint.x - display.width/2, centerPoint.y - display.height/2);
			display.scaleX = starting;
			display.scaleY = starting;
			var startingPos:Point = new Point(centerPoint.x - display.width/2, centerPoint.y - display.height/2);
			
			display.x = startingPos.x;
			display.y = startingPos.y;
			if(ease == null){
				TweenLite.to(display, time, {alpha: endingAlpha, scaleX: ending, scaleY:ending, x: endingPos.x, y: endingPos.y});
			} else {
				TweenLite.to(display, time, {alpha: endingAlpha, scaleX: ending, scaleY:ending, x: endingPos.x, y: endingPos.y, ease: ease});
			}
		}
		
		public static function getButton(label:String,color:uint=0, alpha:Number=1):Sprite{
			var tf:TextField = new TextField();
			Util.applyStyleH1(tf);
			tf.text = label;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.mouseEnabled = false;
			
			var rectSprite:Sprite = getRectSprite(tf.width*1.1,tf.height*1.1,color,alpha);
			rectSprite.addChild(tf);
			rectSprite.buttonMode = true;
			
			tf.x = rectSprite.width/2 - tf.width/2;
			tf.y = rectSprite.height/2 - tf.height/2;
			
			return rectSprite;
		}

		public static function removeChildren(doc:DisplayObjectContainer):void{
			for(var i:int = 0, l:int = doc.numChildren; i < l; i++){
				doc.removeChildAt(0);
			}
		}
	}
}